//
//  XLHomeHeaderView.m
//  industry
//
//  Created by xl on 2020/11/25.
//  Copyright © 2020 Haier. All rights reserved.
//

#import "XLHomeHeaderView.h"
#import "XLHomeIndexCell.h"
#import "XLHomeIndexModel.h"

@interface XLHomeHeaderView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) XLLabel *nameLB;
@property (nonatomic, strong) XLButton *switchBtn;
@property (nonatomic, strong) XLButton *securityBtn;
@property (nonatomic, strong) XLLabel *companyLB;
@property (nonatomic, strong) XLButton *advanceCerBtn;
// 已经移动到弹出菜单中
// @property (nonatomic, strong) XLButton *editBtn;
@property (nonatomic, strong) XLButton *menuBtn;
@property (nonatomic, strong) XLButton *allIndexBtn;
@property (nonatomic, strong) XLCollectionView *collectionView;

// 弹出菜单视图
@property (nonatomic, strong) XLView *menuView;

@property (nonatomic, strong) NSArray *dataSource;

// 是否展示指标数据，默认是展示的；
@property (nonatomic, assign) BOOL showData;

@end
@implementation XLHomeHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.nameLB];
        [self addSubview:self.switchBtn];
        [self addSubview:self.securityBtn];
        [self addSubview:self.companyLB];
        [self addSubview:self.advanceCerBtn];
        // 移动到弹出菜单中
     //   [self addSubview:self.editBtn];
        [self addSubview:self.menuBtn];
        [self addSubview:self.allIndexBtn];
        [self addSubview:self.collectionView];
        
        [self makeConstraints];
    }
    return self;
}

- (void)makeConstraints {
    [_nameLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(35);
        make.top.equalTo(self).offset(30 + STATUSBAR_HEIGHT);
    }];
    [_nameLB sizeToFit];
    
    [_switchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLB.mas_right).offset(12);
        make.centerY.equalTo(self.nameLB);
        make.size.mas_equalTo(CGSizeMake(80, 20));
    }];
    
//    [_securityBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.equalTo(self).offset(-37);
//        make.centerY.equalTo(self.nameLB);
//        make.size.mas_equalTo(CGSizeMake(20, 14));
//    }];
    [_menuBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-37);
        make.centerY.equalTo(self.nameLB);
    }];
    
    [_companyLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLB);
        make.top.equalTo(self.nameLB.mas_bottom).offset(6);
    }];
    
    [_advanceCerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.companyLB);
        make.top.equalTo(self.companyLB.mas_bottom).offset(6);
        make.size.mas_equalTo(CGSizeMake(72, 20));
    }];
    
//    [_editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.nameLB);
//        make.top.equalTo(self.advanceCerBtn.mas_bottom).offset(22);
//        make.size.mas_equalTo(CGSizeMake(16, 16));
//    }];
    
    [_securityBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLB);
        make.top.equalTo(self.advanceCerBtn.mas_bottom).offset(22);
        make.size.mas_equalTo(CGSizeMake(20, 14));
    }];
    
    [_allIndexBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-30);
 //       make.centerY.equalTo(self.editBtn);
        make.centerY.equalTo(self.securityBtn);
    }];
    [_allIndexBtn sizeToFit];
    
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  //      make.top.equalTo(self.editBtn.mas_bottom).offset(10);
        make.top.equalTo(self.securityBtn.mas_bottom).offset(10);
        make.left.bottom.right.equalTo(self);
    }];
}

#pragma mark - Public
- (void)refreshUserInfo:(XLUserInfo *)userInfo {
    self.nameLB.text= userInfo.employeeName;
    self.companyLB.text = ISEmptyString(userInfo.lastLoginCompanyName) ? @"暂未关联公司":userInfo.lastLoginCompanyName;
    if (userInfo.isStaff) {
        _collectionView.hidden = NO;
        _securityBtn.hidden = NO;
        _companyLB.hidden = NO;
        _advanceCerBtn.hidden = NO;
        // 移动到弹出菜单中
 //       _editBtn.hidden = NO;
        _allIndexBtn.hidden = NO;
        [_switchBtn setImage:XLIMAGE(@"qyqh") forState:UIControlStateNormal];
        [_switchBtn mas_updateConstraints:^(MASConstraintMaker *make) {
             make.size.mas_equalTo(CGSizeMake(80, 20));
        }];

    } else {
        _collectionView.hidden = YES;
        _securityBtn.hidden = YES;
        _companyLB.hidden = YES;
        _advanceCerBtn.hidden = YES;
        // 移动到弹出菜单中
        // _editBtn.hidden = YES;
        _allIndexBtn.hidden = YES;
        [_switchBtn setImage:XLIMAGE(@"qhbdyh") forState:UIControlStateNormal];
        [_switchBtn mas_updateConstraints:^(MASConstraintMaker *make) {
             make.size.mas_equalTo(CGSizeMake(89, 20));
        }];

    }

}

- (void)refreshIndexDatas:(NSArray *)arr {
    self.dataSource = arr;
    // 默认关闭眼睛
    self.showData = NO;
    [_collectionView reloadData];
}

#pragma mark - UIButton
- (void)clickSwitchCompany {
    if (self.switchBlock) {
        self.switchBlock();
    }
}

// 移动到弹出菜单中
//- (void)clickEdit {
//    if (self.editBlock) {
//        self.editBlock();
//    }
//}

// 点击“+”菜单，添加全屏菜单视图
- (void)clickMenu {
    [self addMenuView];
}

// 点击空白处，关闭全屏菜单视图
- (void)clickCloseMenu {
    [self removeMenuView];
}

- (void)clickAllIndex {
    if (self.indexBlock) {
        self.indexBlock();
    }
}

// 点击“眼睛”按钮；显示或者隐藏
- (void)clickChangeSecurityDisplay {
    self.showData = !self.showData;
}

// “扫一扫” 菜单按钮
- (void)clickScan {
    [self removeMenuView];
    
    if (self.scanBlock) {
        self.scanBlock();
    }
}

// “自定义指标” 菜单按钮
- (void)clickCustomIndicator {
    [self removeMenuView];
    
    // 就是原来的 “编辑” 按钮
    if (self.editBlock) {
        self.editBlock();
    }
}

#pragma mark - Private
- (void)addMenuView {
    if (self.menuView == nil) {
        // 添加菜单视图到window上，盖住其他组件，防止误操作
        self.menuView = [[XLView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        self.menuView.backgroundColor = [UIColor clearColor];
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        [window addSubview:self.menuView];
        
        // 背景关闭按钮
        XLButton *closeButton = [[XLButton alloc] init];
        closeButton.backgroundColor = [UIColor clearColor];
        [closeButton addTarget:self action:@selector(clickCloseMenu) forControlEvents:UIControlEventTouchUpInside];
        [self.menuView addSubview:closeButton];
        [closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.right.equalTo(0);
        }];
        
        // 辅助视图，用来定位；
        XLView *tempView = [[XLView alloc] init];
        tempView.backgroundColor = [UIColor clearColor];
        [self.menuView addSubview:tempView];
        CGRect frame = [self convertRect:self.menuBtn.frame toView:self.menuView];
        tempView.frame = CGRectMake(frame.origin.x, frame.origin.y + frame.size.height -1 , frame.size.width, 1);
        
        // 容器视图
        XLView *containerView = [[XLView alloc] init];
        containerView.backgroundColor = COLOR_WITH_HEX(0x444444);
        containerView.layer.cornerRadius = 4;
        [self.menuView addSubview:containerView];
        [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(tempView.mas_bottom).offset(10);
            make.right.mas_equalTo(-23);
            make.size.mas_equalTo(CGSizeMake(130, 77));
        }];
        
        // 三角形箭头
        XLImageView *triangleImageView = [[XLImageView alloc] init];
        triangleImageView.image = [UIImage imageNamed:@"triangle"];
        [self.menuView addSubview:triangleImageView];
        [triangleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(tempView.mas_centerX);
            make.bottom.equalTo(containerView.mas_top);
            make.size.equalTo(CGSizeMake(10, 5));
        }];
        
        // 分隔线
        XLView *lineView = [[XLView alloc] init];
        lineView.backgroundColor = COLOR_WITH_HEX(0x575757);
        [containerView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(0);
            make.left.equalTo(39);
            make.right.equalTo(0);
            make.height.equalTo(1);
        }];
        
        // 上下容器
        XLView *topView = [[XLView alloc] init];
        topView.backgroundColor = [UIColor clearColor];
        [containerView addSubview:topView];
        [topView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(0);
            make.bottom.equalTo(lineView.mas_top);
        }];
        
        XLView *bottomView = [[XLView alloc] init];
        bottomView.backgroundColor = [UIColor clearColor];
        [containerView addSubview:bottomView];
        [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.equalTo(0);
            make.top.equalTo(lineView.mas_bottom);
        }];
        
        // 扫一扫
        XLButton *scanButton = [[XLButton alloc] init];
        [scanButton addTarget:self action:@selector(clickScan) forControlEvents:UIControlEventTouchUpInside];
        [topView addSubview:scanButton];
        [scanButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.right.equalTo(0);
        }];
        
        XLImageView *scanImageView = [[XLImageView alloc] init];
        scanImageView.image = [UIImage imageNamed:@"scan"];
        [topView addSubview:scanImageView];
        [scanImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(0);
            make.left.equalTo(13);
            make.size.equalTo(CGSizeMake(14, 14));
        }];
        
        XLLabel *scanLabel = [[XLLabel alloc] init];
        scanLabel.text = @"扫一扫";
        scanLabel.font = [UIFont fontOfPingFangSCRegularWithSize:14];
        scanLabel.textColor = COLOR_WITH_HEX(0xFFFFFF);
        [topView addSubview:scanLabel];
        [scanLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(0);
            make.left.equalTo(40);
        }];
        
        // 自定义指标
        XLButton *customIndicatorButton = [[XLButton alloc] init];
        [customIndicatorButton addTarget:self action:@selector(clickCustomIndicator) forControlEvents:UIControlEventTouchUpInside];
        [bottomView addSubview:customIndicatorButton];
        [customIndicatorButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.right.equalTo(0);
        }];
        
        XLImageView *customIndicatorImageView = [[XLImageView alloc] init];
        customIndicatorImageView.image = [UIImage imageNamed:@"custom_indicator"];
        [bottomView addSubview:customIndicatorImageView];
        [customIndicatorImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(0);
            make.left.equalTo(13);
            make.size.equalTo(CGSizeMake(14, 14));
        }];
        
        XLLabel *customIndicatorLabel = [[XLLabel alloc] init];
        customIndicatorLabel.text = @"自定义指标";
        customIndicatorLabel.font = [UIFont fontOfPingFangSCRegularWithSize:14];
        customIndicatorLabel.textColor = COLOR_WITH_HEX(0xFFFFFF);
        [bottomView addSubview:customIndicatorLabel];
        [customIndicatorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(0);
            make.left.equalTo(40);
        }];
    }
}

- (void)removeMenuView {
    // 将菜单视图从window上移除
    [self.menuView removeFromSuperview];
    self.menuView = nil;
}

#pragma mark - UICollectionViewDataSource & UICollectionViewDelegateFlowLayout
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = KIsiPhonexPlus ? CGSizeMake(121, 154) : CGSizeMake(110, 140);
    return size;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
     return UIEdgeInsetsMake(0, 30, 0, 30);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 15.0f;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 15.0f;
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row > self.dataSource.count - 1) {
        return [XLCollectionViewCell new];
    }
    XLHomeIndexCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"XLHomeIndexCell" forIndexPath:indexPath];
    [cell loadCellData:self.dataSource[indexPath.row] index:indexPath.row];
    return cell;
}

#pragma mark - setter/getter
- (XLLabel *)nameLB {
    if (!_nameLB) {
        _nameLB = [XLLabel new];
        _nameLB.font = [UIFont fontOfPingFangSCSemiboldWithSize:22];
        _nameLB.textColor = [UIColor blackFontColor];
    }
    return _nameLB;
}

- (XLButton *)switchBtn {
    if (!_switchBtn) {
        _switchBtn = [XLButton new];
        [_switchBtn setImage:XLIMAGE(@"qyqh") forState:UIControlStateNormal];
        [_switchBtn addTarget:self action:@selector(clickSwitchCompany) forControlEvents:UIControlEventTouchUpInside];
    }
    return _switchBtn;
}

- (XLButton *)securityBtn {
    if (!_securityBtn) {
        _securityBtn = [XLButton new];
        [_securityBtn setImage:XLIMAGE(@"security") forState:UIControlStateNormal];
        [_securityBtn addTarget:self action:@selector(clickChangeSecurityDisplay) forControlEvents:UIControlEventTouchUpInside];
    }
    return _securityBtn;
}

- (void)setShowData:(BOOL)showData {
    // 修改dataSource
    @synchronized (self) {
        _showData = showData;
        if (showData) {
            [_securityBtn setImage:XLIMAGE(@"security") forState:UIControlStateNormal];
        } else {
            [_securityBtn setImage:XLIMAGE(@"security_close") forState:UIControlStateNormal];
        }
        [self.dataSource enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            ((XLHomeIndexModel *)obj).hideData = !showData;
        }];
        [_collectionView reloadData];
    }
}

- (XLLabel *)companyLB {
    if (!_companyLB) {
        _companyLB = [XLLabel new];
        _companyLB.font = [UIFont fontOfPingFangSCSemiboldWithSize:14];
        _companyLB.textColor = [UIColor blackFontColor];
    }
    return _companyLB;
}

- (XLButton *)advanceCerBtn {
    if (!_advanceCerBtn) {
        _advanceCerBtn = [XLButton new];
        [_advanceCerBtn setImage:XLIMAGE(@"rz") forState:UIControlStateNormal];
        [_advanceCerBtn setTitle:@"高级认证" forState:UIControlStateNormal];
        _advanceCerBtn.userInteractionEnabled = NO;
    }
    return _advanceCerBtn;
}

// 移动到弹出菜单中
//- (XLButton *)editBtn {
//    if (!_editBtn) {
//        _editBtn = [XLButton new];
//        [_editBtn setImage:XLIMAGE(@"bj") forState:UIControlStateNormal];
//        [_editBtn addTarget:self action:@selector(clickEdit) forControlEvents:UIControlEventTouchUpInside];
//    }
//    return _editBtn;
//}

- (XLButton *)menuBtn {
    if (!_menuBtn) {
        _menuBtn = [XLButton new];
        [_menuBtn setImage:XLIMAGE(@"menu") forState:UIControlStateNormal];
        [_menuBtn addTarget:self action:@selector(clickMenu) forControlEvents:UIControlEventTouchUpInside];
    }
    return _menuBtn;
}

- (XLButton *)allIndexBtn {
    if (!_allIndexBtn) {
        _allIndexBtn = [XLButton new];
        _allIndexBtn.titleLabel.font = [UIFont fontOfPingFangSCRegularWithSize:14];
        [_allIndexBtn setTitleColor:[UIColor blackFontColor] forState:UIControlStateNormal];
        NSString *title = @"全部指标";
        NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:title];
        [attri addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, title.length)];
        [_allIndexBtn setAttributedTitle:attri forState:UIControlStateNormal];
        [_allIndexBtn addTarget:self action:@selector(clickAllIndex) forControlEvents:UIControlEventTouchUpInside];
    }
    return _allIndexBtn;
}

- (XLCollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
        flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[XLCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flow];
        _collectionView.showsHorizontalScrollIndicator = NO;
        // 这里原本禁用了回弹效果，原因不明；根据需求，需要这个回弹效果
        _collectionView.bounces = YES;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        //注册cell
        [_collectionView registerClass:[XLHomeIndexCell class] forCellWithReuseIdentifier:@"XLHomeIndexCell"];
    }
    return _collectionView;
}
@end
